package com.ias.agdis_r.view.box;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BoxActivity;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.manager.DataManager;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.ServiceManage;

public class BoxView extends ImageView implements OnClickListener {
    private boolean mAttached = false;
    public static final String BOX_REFRESH = "BOX_REFRESH";

    public BoxView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.setOnClickListener(this);
    }

    public BoxView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.setOnClickListener(this);
    }

    public BoxView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        this.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //如果未登录提示登录
        if (ServiceManage.getInstance().getIsLogon()) {
            //删除Activity，重新生成实例
            DataBaseManager.getInstance().removeBoxActivty();
            Intent intent = new Intent(getContext(),
                    BoxActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getContext().startActivity(intent);
        } else {
            EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, getContext().getString(R.string.use_function_tologin), false);
            ServiceManage.getInstance().startLoginActivity();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!mAttached) {
            mAttached = true;
            IntentFilter filter = new IntentFilter();
            filter.addAction(BOX_REFRESH);
            getContext().registerReceiver(mIntentReceiver, filter);
        }
        refresh();
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasWindowFocus);
        if (getVisibility() == View.VISIBLE) {
            refresh();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        // TODO Auto-generated method stub
        super.onDetachedFromWindow();
        if (mAttached) {
            getContext().unregisterReceiver(mIntentReceiver);
            mAttached = false;
        }
    }

    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BOX_REFRESH.equals(action)) {
                refresh();
            }
        }
    };

    /**
     * 刷新提示数量
     */
    public void refresh() {
//		setBadgeCount(DataManager.getInstance().getUnCheckNumber());
        if (0 != DataManager.getInstance().getUnCheckNumber()) {
            startFlick();
        } else {
            stopFlick();
        }
    }

    /**
     * 开启View闪烁效果
     */
    private void startFlick() {
        Animation alphaAnimation = new AlphaAnimation(1, 0);
        alphaAnimation.setDuration(800);
        alphaAnimation.setInterpolator(new LinearInterpolator());
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        setImageResource(R.drawable.email_flick);
        startAnimation(alphaAnimation);
    }

    /**
     * 取消View闪烁效果
     */
    private void stopFlick() {
        clearAnimation();
        setImageResource(R.drawable.email_normal);
    }
}
